﻿Imports Runa.UI.Controls
Imports Runa.UI.Threading
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework
Imports Runa.Foundation.Messaging
Imports Runa.Core.Services
Imports Microsoft.Xna.Framework.Content
Imports Runa.Resources.ContentManagers

Namespace Global.Runa.UI.Compoents

    Public Class FPS
        Inherits DrawableGameComponent

        Private content As ResourceStreamContentManager
        Private _ContentRoot As String
        Private elapsedTime As TimeSpan
        Private frameCounter As Integer
        Private frameRate As Integer
        Private spriteBatch As SpriteBatch
        Private spriteFont As SpriteFont

        Public Sub New(ByVal game As Game, ByVal spriteBatch As SpriteBatch)
            MyBase.New(game)
            Me.DrawOrder = Integer.MaxValue
            Me.frameRate = 0
            Me.frameCounter = 0
            Me.elapsedTime = TimeSpan.Zero
            Me._ContentRoot = GetType(FPS).Assembly.GetName().Name
            Me.content = New ResourceStreamContentManager(game.Services, Me._ContentRoot, GetType(FPS).Assembly)
            Me.LoadGraphicsContent(True)
            Me.spriteBatch = spriteBatch
        End Sub

        Public Overrides Sub Draw(ByVal gameTime As GameTime)
            Me.elapsedTime = (Me.elapsedTime + gameTime.ElapsedGameTime)
            If (Me.elapsedTime > TimeSpan.FromSeconds(1)) Then
                Me.elapsedTime = (Me.elapsedTime - TimeSpan.FromSeconds(1))
                Me.frameRate = Me.frameCounter
                Me.frameCounter = 0
            End If
            Me.frameCounter += 1
            Dim fps As String = String.Format("FPS: {0}", Me.frameRate)
            Me.spriteBatch.Begin()
            Me.spriteBatch.DrawString(Me.spriteFont, fps, New Vector2(20.0!, 20.0!), Color.White)
            Me.spriteBatch.End()
        End Sub

        Protected Sub LoadGraphicsContent(ByVal loadAllContent As Boolean)
            If loadAllContent Then
                Me.spriteFont = Me.content.Load(Of SpriteFont)("HudFont.xnb")
            End If
        End Sub

    End Class

End Namespace